package ACWing.DataStructure.并查集;
//837. 连通块中点的数量

import java.util.Scanner;

/**
 * @author :chenjie
 * @date :Created 2022/12/29 15:30
 */
public class TheNumberOFPointsInTheConnectedBlock {
    static int[]p=new int[100010];
    static int[]size=new int[100010];//保存的是祖宗节点的节点数量
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        sc.nextLine();
        for (int i = 1; i <= n; i++) {//初始化使父节点都为本身
            p[i]=i;
            size[i]=1;
        }
        while (m-->0){
            String s=sc.next();
            if("C".equals(s)){
                int x=sc.nextInt();
                int y=sc.nextInt();
                int i = find(x);
                int i1 = find(y);
                if(i==i1){
                    continue;
                }
                p[i]=i1;
                size[i1]+=size[i];
            }
            if("Q1".equals(s)){
                int x=sc.nextInt();
                int y=sc.nextInt();
                if(find(x)==find(y)){
                    System.out.println("Yes");
                }else{
                    System.out.println("No");
                }
            }
            if("Q2".equals(s)){
                int x=sc.nextInt();

                System.out.println(size[find(x)]);
            }
        }

    }
    public static int find(int x){//理解为查找祖宗节点+路径压缩
        if (p[x]!=x){
            p[x]=find(p[x]);
        }
        return p[x];
    }
}
